{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "/Users/sibanjan/anaconda/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "import matplotlib.pylab as plt\n",
    "from keras.layers import Dense, Flatten\n",
    "from keras.layers import Conv2D, MaxPooling2D\n",
    "from keras.models import Sequential\n",
    "from keras.datasets import mnist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train data shape: (60000, 28, 28, 1)\n",
      "Number of train samples 60000\n",
      "Number of test samples 10000\n"
     ]
    }
   ],
   "source": [
    "# Define the image's input dimensions\n",
    "image_x, image_y = 28, 28\n",
    "\n",
    "# Load the mnist dataset for the keras sample datasets library\n",
    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
    "\n",
    "# reshape the data\n",
    "x_train = x_train.reshape(x_train.shape[0], image_x, image_y, 1)\n",
    "x_test = x_test.reshape(x_test.shape[0], image_x, image_y, 1)\n",
    "input = (image_x, image_y, 1)\n",
    "\n",
    "x_train = x_train.astype('float32')\n",
    "x_test = x_test.astype('float32')\n",
    "x_train /= 255\n",
    "x_test /= 255\n",
    "print('Train data shape:', x_train.shape)\n",
    "print('Number of train samples',x_train.shape[0])\n",
    "print('Number of test samples',x_test.shape[0])\n",
    "\n",
    "num_classes = 10\n",
    "\n",
    "y_train = keras.utils.to_categorical(y_train, num_classes)\n",
    "y_test = keras.utils.to_categorical(y_test, num_classes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Model Definition\n",
    "cnn_model = Sequential()\n",
    "cnn_model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1),activation='relu',input_shape=input))\n",
    "cnn_model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))\n",
    "cnn_model.add(Flatten())\n",
    "cnn_model.add(Dense(1000, activation='relu'))\n",
    "cnn_model.add(Dense(num_classes, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cnn_model.compile(loss=keras.losses.categorical_crossentropy,\n",
    "              optimizer=keras.optimizers.Adam(),\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 10000 samples\n",
      "Epoch 1/10\n",
      "60000/60000 [==============================] - 566s - loss: 0.0994 - acc: 0.9697 - val_loss: 0.0685 - val_acc: 0.9779\n",
      "Epoch 2/10\n",
      "60000/60000 [==============================] - 533s - loss: 0.0406 - acc: 0.9879 - val_loss: 0.0501 - val_acc: 0.9835\n",
      "Epoch 3/10\n",
      "60000/60000 [==============================] - 554s - loss: 0.0240 - acc: 0.9925 - val_loss: 0.0451 - val_acc: 0.9877\n",
      "Epoch 4/10\n",
      "60000/60000 [==============================] - 577s - loss: 0.0172 - acc: 0.9945 - val_loss: 0.0551 - val_acc: 0.9872\n",
      "Epoch 5/10\n",
      "60000/60000 [==============================] - 558s - loss: 0.0152 - acc: 0.9960 - val_loss: 0.0580 - val_acc: 0.9877\n",
      "Epoch 6/10\n",
      "60000/60000 [==============================] - 692s - loss: 0.0121 - acc: 0.9968 - val_loss: 0.0787 - val_acc: 0.9851\n",
      "Epoch 7/10\n",
      "60000/60000 [==============================] - 59789s - loss: 0.0108 - acc: 0.9974 - val_loss: 0.0739 - val_acc: 0.9878\n",
      "Epoch 8/10\n",
      "60000/60000 [==============================] - 561s - loss: 0.0101 - acc: 0.9976 - val_loss: 0.0855 - val_acc: 0.9866\n",
      "Epoch 9/10\n",
      "60000/60000 [==============================] - 1463s - loss: 0.0083 - acc: 0.9979 - val_loss: 0.0983 - val_acc: 0.9855\n",
      "Epoch 10/10\n",
      "60000/60000 [==============================] - 537s - loss: 0.0098 - acc: 0.9980 - val_loss: 0.0924 - val_acc: 0.9875\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x11c954898>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnn_model.fit(x_train, y_train,\n",
    "          batch_size=10,\n",
    "          epochs=10,\n",
    "          verbose=1,\n",
    "          validation_data=(x_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loss on the test data: 0.09238649257670754\n",
      "Model accuracy on the test data: 0.9875\n"
     ]
    }
   ],
   "source": [
    "model_score = cnn_model.evaluate(x_test, y_test, verbose=0)\n",
    "print('Loss on the test data:', model_score[0])\n",
    "print('Model accuracy on the test data:', model_score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
